package com.health.ts.controller.consultation;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.consultation.PatientInfo;
import com.health.ts.service.consultation.PatientInfoService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;
/**  
* @ClassName: patientController  
* @Description:患者管理
*/  
@RestController
@RequestMapping(value = "/api/patient",produces= BaseController.JSON_UTF8)
public class PatientController extends BaseController{
	
	@Autowired
	PatientInfoService patientInfoService;
	
	/**  
	* @Title: addPatient  
	* @Description: 新增
	* @param @param patientInfo
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ApiResult addPatient(@RequestBody PatientInfo patientInfo) {
		patientInfo.setId(GenerateIdUtil.makeID());
		patientInfo.setIsTran("0");//未转诊
		patientInfo.setIsFlup("1");//未随访
		patientInfo.setCreate_time(new Date());
		patientInfoService.save(patientInfo);
		return ApiResult.success();
	}
	
	/**  
	* @Title: updatePatient  
	* @Description: 修改  
	* @param @param id
	* @param @param patientInfo
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/update/{id}", method=RequestMethod.PUT)
	public ApiResult updatePatient(@PathVariable String id,@RequestBody PatientInfo patientInfo) {
		PatientInfo info = patientInfoService.getById(id);
		AssertUtil.notNull(info, "要修改的患者不存在！");
		patientInfo.setId(id);
		patientInfo.setUpdate_time(new Date());
		patientInfoService.updateById(patientInfo);
		return ApiResult.success();
	}
	
	/**  
	* @Title: deletePatient  
	* @Description: 删除 
	* @param @param id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE)
	public ApiResult deletePatient(@PathVariable String id) {
		patientInfoService.removeById(id);
		return ApiResult.success();
	}
	
	/**  
	* @Title: selectPatient  
	* @Description: 根据id查询  
	* @param @param id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/select/{id}", method=RequestMethod.GET)
	public ApiResult selectPatient(@PathVariable String id) {
		PatientInfo patientInfo=patientInfoService.getById(id);
		AssertUtil.notNull(patientInfo, "要查询的患者不存在！");
		return ApiResult.success(patientInfo);
	}
	
	/**  
	* @Title: selectList  
	* @Description: 查询 
	* @param @param page
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public ApiResult selectList(FrontPage page) {
		QueryWrapper<PatientInfo> wrapper = new QueryWrapper<PatientInfo>();
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.like("idnumber", idnumber); 
		}
		wrapper.eq("isTran", "0");
		Page<PatientInfo> pageList = patientInfoService.page(page.getPagePlus(), wrapper);
		CustomPage<PatientInfo> customPage = new CustomPage<PatientInfo>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**  
	* @Title: selectList  
	* @Description: 查询 
	* @param @param page
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/listFlup", method=RequestMethod.POST)
	public ApiResult selectListFlup(FrontPage page) {
		QueryWrapper<PatientInfo> wrapper = new QueryWrapper<PatientInfo>();
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.like("idnumber", idnumber); 
		}
		wrapper.eq("isFlup", "1");
		Page<PatientInfo> pageList = patientInfoService.page(page.getPagePlus(), wrapper);
		CustomPage<PatientInfo> customPage = new CustomPage<PatientInfo>(pageList);
		return ApiResult.success(customPage);
	}
}
